package com.qst.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.survey.entity.Question;
import com.qst.survey.entity.QuestionAnswer;
import com.qst.survey.entity.QuestionTopic;
import com.qst.survey.entity.Replay;
import com.qst.survey.mapper.QuestionAnswerMapper;
import com.qst.survey.mapper.QuestionMapper;
import com.qst.survey.mapper.QuestionTopicMapper;
import com.qst.survey.pojo.TopicData;
import com.qst.survey.service.AnswerService;

import com.qst.survey.service.QuestionTopicService;
import com.qst.survey.service.ReplayService;
import com.qst.survey.util.FastDFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Queue;

/**
 * @Date: 2021/3/18 20:36
 * @Description: QuestionTopic的Service具体实现层方法
 */
@Service
public class QuestionTopicServiceImpl implements QuestionTopicService {

    @Autowired
    QuestionTopicMapper questionTopicMapper;

    @Autowired
    QuestionAnswerMapper questionAnswerMapper;

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ReplayService replayService;

    @Autowired
    FastDFSUtil fastDFSUtil;

    @Autowired
    AnswerService answerService;


    /**
     * @param questionTopic:
     * @return void
     * @description: TODO 题目添加方法
     * @date 20:41:44 2021/3/18
     */
    @Override
    @Transactional
    public Integer addTopic(QuestionTopic questionTopic) {
        //存入问题
        questionMapper.topicNumbPlusOne(questionTopic.getQuestionId());
        int insertId = questionTopicMapper.insert(questionTopic);
        //存入题目信息
        if (questionTopic.getAnswer() != null && questionTopic.getAnswer().size() > 0) {
            questionTopic.setAnswerType(questionTopic.getAnswer().size());
            insertId = answerService.addAnswer(questionTopic.getId(), questionTopic.getAnswer());
        }
        if (insertId > 0) {
            questionMapper.topicNumbPlusOne(questionTopic.getQuestionId());
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * @param topicId:要查询的id
     * @return java.lang.Integer
     * @description: TODO 查找题号
     * @date 21:29:20 2021/3/22
     */
    /**
     * @param topicId:
     * @return java.util.HashMap
     * @description: TODO 查找一个题目的题目和选项
     * @date 22:20:11 2021/3/22
     */
    @Override
    public QuestionTopic findTopicAll(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", topicId);
        QuestionTopic questionTopic = questionTopicMapper.selectOne(queryWrapper);
        List<QuestionAnswer> topicAllAnswer = answerService.findTopicAllAnswer(topicId);
        questionTopic.setAnswers(topicAllAnswer);
        return questionTopic;
    }

    /**
     * @param topicId ：题目id
     * @return java.lang.Integer
     * @description TODO  通过题目id来删除题目
     * @date 2021/4/2
     */
    @Override
    public Integer deleteTopic(Integer topicId) {
        int i = questionTopicMapper.deleteById(topicId);
        return i;
    }

    /**
     * @param topic ：问卷题目id
     * @return java.lang.Integer
     * @description TODO   更新问题题目
     * @date 2021/4/2
     */
    @Override
    public Integer updateTopic(QuestionTopic topic) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", topic.getId());
        int update = questionTopicMapper.update(topic, queryWrapper);
        return update;
    }

    /**
     * @param topicId ：问卷题目id
     * @return com.qst.survey.pojo.TopicData
     * @description TODO   根绝Id查找一个题目的数据
     * @date 2021/4/2
     */
    @Override
    public TopicData findTopicData(Integer topicId) {
        QuestionTopic questionTopic = questionTopicMapper.selectById(topicId);
        //如果是一道问答题
        if (questionTopic.getAnswerType() == 0) {
            com.baomidou.mybatisplus.extension.plugins.pagination.Page page = new Page<Replay>(1, -1);
            Page<Replay> pageById = replayService.findPageById(page, topicId);
            TopicData topicData = new TopicData();
            topicData.setTopicId(questionTopic.getId());
            topicData.setTopicType(questionTopic.getAnswerType());
            topicData.setData(pageById.getRecords());
            topicData.setTopicTitle(questionTopic.getTitle());
            topicData.setAnswerTotal(pageById.getTotal());
            topicData.setCurrentPage(1);
            return topicData;
        } else if (questionTopic.getAnswerType() >= 1) {  //如果是一道选择题
            List<QuestionAnswer> answerData = answerService.findAnswerData(topicId);
            TopicData topicData = new TopicData();
            topicData.setTopicId(questionTopic.getId());
            topicData.setTopicType(questionTopic.getAnswerType());
            topicData.setData(answerData);
            topicData.setTopicTitle(questionTopic.getTitle());
            topicData.setAnswerTotal(answerData.size());
            long total = 0;
            for (QuestionAnswer answer : answerData
            ) {
                total = total + answer.getSelected();
            }
            topicData.setSelectTotal(total);
            return topicData;
        } else {
            return null;
        }
    }
    /**
     * @param paperId 问卷id
     * @return java.util.List
     * @description TODO 通过所有页面id来查出所有的数据
     * @date 2021/4/2
     */
    @Override
    public List findAllData(Integer paperId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", paperId);
        List list = questionTopicMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * @param id ：问卷id
     * @return java.util.List<com.qst.survey.entity.QuestionTopic>
     * @description TODO 通过问卷id来查出所有的题目
     * @date 2021/4/2
     */
    @Override
    public List<QuestionTopic> findById(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", id);
        return questionTopicMapper.selectList(queryWrapper);
    }
}
